package com.wkf.truck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wkf.truck.common.CommonEnum;
import com.wkf.truck.model.Truck;
import com.wkf.truck.dao.TruckDao;
import com.wkf.truck.model.User;
import com.wkf.truck.service.TruckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wkf.truck.service.UserService;
import com.wkf.truck.vo.TruckTop10Vo;
import com.wkf.truck.vo.TruckUserVo;
import com.wkf.truck.vo.TruckVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lxb
 * @since 2021-02-14
 */
@Service
public class TruckServiceImpl extends ServiceImpl<TruckDao, Truck> implements TruckService {


    @Autowired
    private TruckDao truckDao;

    @Autowired
    private UserService userService;

    @Override
    public TruckVo getInfo(Long truckId) {
        Truck truck = truckDao.selectOne(new QueryWrapper<Truck>().eq("id", truckId));

        if (truck == null) {
            return null;
        }
        TruckVo truckVo = new TruckVo();
        truckVo.setTruckId(truck.getId());
        truckVo.setTruckType(truck.getType());
        truckVo.setTruckDriver(truck.getDriver());
        truckVo.setTruckSpace(truck.getSpace());
        truckVo.setTruckCapacity(truck.getCapacity());
        truckVo.setTruckImage(truck.getImg());
        truckVo.setTruckAdvice(truck.getAdvice());
        truckVo.setTruckNumber(truck.getNumber());

        Long userId = truck.getUserId();
        User user = userService.getOne(new QueryWrapper<User>().eq("id", userId));
        if (user == null) {
            return null;
        }
        truckVo.setOrderNumber(user.getOrderNumber());

        return truckVo;
    }

    @Override
    @Transactional
    public List<TruckTop10Vo> getTop10() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.orderByDesc("order_number");
        wrapper.eq("role_id", CommonEnum.Role.商家.getCode());
        wrapper.last("limit 10");
        List<User> userList = userService.list(wrapper);

        List<TruckTop10Vo> top10VoList = userList.stream().filter(item -> {
            return item.getTruckId() != null;
        }).map(item -> {
            Truck truck = truckDao.selectOne(new QueryWrapper<Truck>().eq("user_id", item.getId()));
            TruckTop10Vo top10Vo = new TruckTop10Vo();
            if (truck == null) {
                return null;
            }

            top10Vo.setAdvice(truck.getAdvice());
            top10Vo.setTruckId(truck.getId());
            top10Vo.setOrderNumber(item.getOrderNumber());
            top10Vo.setTruckDriver(truck.getDriver());
            top10Vo.setTruckType(truck.getType());
            return top10Vo;
        }).collect(Collectors.toList());

        return top10VoList;
    }

    @Override
    public Page<TruckUserVo> getTruckUsers(Truck truck) {
        Page<TruckUserVo> page = new Page<>(truck.getPageNum(),truck.getPageSize());
        Page result = truckDao.getTruckUsers(page,truck);
        return result;
    }
}
